package com.zbl.zblproject.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.erp.entity.ErpWarehouse;
import com.zbl.zblproject.erp.mapper.ErpWarehouseMapper;
import com.zbl.zblproject.erp.service.ErpWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Min;
import java.util.List;


/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/9
 * 实现了仓库管理的API接口
 */
@Api(value = "ERP仓库管理", tags = {"ERP仓库管理"})
@RestController
@RequestMapping("/erp-warehouse")
public class ErpWarehouseController {

    @Autowired
    private ErpWarehouseService erpWarehouseService;
    @Autowired
    private ErpWarehouseMapper warehouseMapper;

    @GetMapping("/list")
    @ApiOperation("获取所有启用的仓库")
    public Result list(){
        return ResultUtil.success("成功!", erpWarehouseService.allHouseware(0));
    }

    @GetMapping("/list/{search}")
    @ApiOperation("查询所有可用启用的仓库")
    public Result listOfSearch(@ApiParam("查询条件") @PathVariable String search){
        return ResultUtil.success("成功!", erpWarehouseService.searchGoods(search));
    }


    @GetMapping("/shelves/{whname}")
    @ApiOperation("获取仓库的启用架位")
    public Result listShelves(@ApiParam("仓库名称") @PathVariable String whname){
        return ResultUtil.success("成功!", erpWarehouseService.shelves(whname, 0));
    }

    @GetMapping("/goods/{whname}/{shelves}")
    @ApiOperation("获取对应仓库架位的货位")
    public Result listGoods(
            @ApiParam("仓库名称") @PathVariable String whname,
            @ApiParam("架位名称") @PathVariable String shelves
    ){
        return ResultUtil.success("成功!", erpWarehouseService.goods(whname, shelves, 0));
    }

    @PostMapping("/add")
    @ApiOperation("新增一个仓库")
    public Result addWarehouse(@Validated ErpWarehouse erpWarehouse){
        try {
            if (erpWarehouseService.addErpWarehouse(erpWarehouse)) {
                return ResultUtil.success("成功!", erpWarehouse);
            }
            return ResultUtil.error("失败!");
        }catch(Exception e){
            return ResultUtil.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据ID来删除对应的仓库")
    public Result deleteWarehouse(@ApiParam("仓库ID") @PathVariable("id") int id){
        if(id < 1){
            return ResultUtil.error("仓库ID异常.");
        }
        if(erpWarehouseService.deleteErpWarehouse(id)){
            return ResultUtil.success("删除成功!");
        }
        return ResultUtil.error("删除失败!");
    }

    @PostMapping("/update")
    @ApiOperation("根据ID来更新仓库")
    public Result updateWarehouse(@Validated ErpWarehouse warehouse){
        if(warehouse.getId() < 1){
            return ResultUtil.error("仓库ID异常");
        }
        if(erpWarehouseService.updateErpWarehouse(warehouse)){
            return ResultUtil.success("更新成功!");
        }
        return ResultUtil.error("删除失败!");
    }

    @GetMapping("/selectPs")
    @ApiOperation("破损仓库")
    public Result selectPs(){
        String ps = warehouseMapper.selectPs();
        return ResultUtil.success("成功!", ps);
    }

    @GetMapping("/list/{current}/{size}")
    @ApiOperation("仓库列表")
    public Result listWarehouse(@Min(value = 1, message = "页数异常") @ApiParam("查询页数") @PathVariable int current,
                                @Min(value = 1, message = "数量异常") @ApiParam("查询数量") @PathVariable int size,
                                @ApiParam("查询仓库名称") @RequestParam("search") String search){
        IPage<ErpWarehouse> page = erpWarehouseService.page(search, current, size);
        return ResultUtil.success("成功!", page);
    }

    @GetMapping("/group-list/{current}/{size}/{mode}")
    @ApiOperation("分组仓库列表")
    public Result groupListWarehouse(@ApiParam("查询页数") @PathVariable int current,
                                     @ApiParam("查询数量") @PathVariable int size,
                                     @ApiParam("名称") @RequestParam("search") String search,
                                     @PathVariable int mode){
        IPage<JSONObject> page = erpWarehouseService.pageGroup(search, current, size,mode);

        return ResultUtil.success("成功!", page);
    }


    @GetMapping("/desc/{id}")
    @ApiOperation("仓库商品详情")
    public Result desc(@ApiParam("仓库ID") @PathVariable Integer id){
        if(id <= 0){
            return ResultUtil.error("ID查询异常");
        }
        return ResultUtil.success("成功", erpWarehouseService.getItemByWhId(id));
    }



    @GetMapping("/goods/{id}")
    @ApiOperation("获取商品的货位信息")
    public Result warehouseItem(@ApiParam("商品ID") @PathVariable Integer id){
        if(id <= 0){
            return ResultUtil.error("ID查询异常");
        }
        return ResultUtil.success("成功", erpWarehouseService.getItemByGoodsId(id));
    }


    @GetMapping("/update-status/{id}")
    @ApiOperation("更新仓库状态")
    public Result updateStatusItem(@ApiParam("仓库ID") @PathVariable int id){
        if(erpWarehouseService.updateStatusForWarehouse(id)){
            return ResultUtil.success("成功!");
        }else{
            return ResultUtil.error("仓位被占用");
        }
    }


    @PostMapping("/uploadWh")
    @ApiOperation("提交xls,goods自动更新进入数据库")
    public Result uploadWh(@RequestParam("file") MultipartFile file) {
        try {
            System.out.println(file.getOriginalFilename());
            return erpWarehouseService.updateWhToDatabase(file);
        } catch (NullPointerException e) {
            return ResultUtil.error("一些字段为空");
        } catch (RuntimeException e) {
            return ResultUtil.error(e.getMessage());
        }
    }

    @GetMapping("/print-list")
    @ApiOperation("打印列表")
    public Result goodsList(Integer[] ids) {
        JSONArray array = erpWarehouseService.printList(ids);
        if (array.isEmpty()) {
            return ResultUtil.error("没有数据");
        } else {
            return ResultUtil.success("成功", array);
        }
    }
}
